﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ParamSetCollection.General.Parameters.Normal;
using ParamSetCollection.General.Logs;


namespace MainController.Hardware.PLC.MEGMEET.Datas.Groups
{
    class ShortGroup : ParamGroup
    {
        #region============================== 变量 ====================================
        //public override event OnRenewParamListEventHandler OnRenewParamListEvent;

        static bool mytest = false;

        static Dictionary<int, List<string>> posiList = new Dictionary<int, List<string>>();
        #endregion=====================================================================


        #region============================== 属性 ====================================
        #endregion=====================================================================


        #region============================= 内部函数 ================================
        private string CheckBoolValue(string val)
        {
            try
            {
                if (val.IndexOf('r') > 0)
                {
                    return "1";
                }
                else if (val.IndexOf('s') > 0)
                {
                    return "0";
                }
                else
                {
                    return val;
                }
            }
            catch (Exception)
            {

                return "0";
            }
        }

        private void BindingDataUnit(List<DataUnit> duList)
        {
            foreach (string item in m_paramDic.Keys)
            {
                int pos = Convert.ToInt32(m_paramDic[item].Position);



            }

        }
        #endregion====================================================================


        #region============================= 保护函数 ================================
        //修改data缓存值
        protected override void ChangeBuffer(string position, object value)
        {
            data[Convert.ToInt32(position)] = (ushort)value;
        }
        #endregion====================================================================


        #region============================= 公开函数 ================================
        public ShortGroup(string paramType, string baseCode,  Dictionary<string, string> paramDic, string cfgPath, ushort[] dataBuf)
            : base(paramType, baseCode, "short.cfg",  paramDic, cfgPath, dataBuf)
        {

        }

        public override List<CommUnit> GetParamSetting(ref List<ParamUnit> paramList)//
        {
            List<CommUnit> cmdList = new List<CommUnit>();
            List<ParameterUnit> _paramList = new List<ParameterUnit>();
            foreach (ParamUnit item in paramList)
            {
                if (m_paramDic.ContainsKey(item.Code))
                {
                    string val = CheckBoolValue(item.m_value).ToString();
                    if (m_paramDic[item.Code].Value == val)
                    {
                        continue;
                    }

                    //m_paramDic[item.Code].Value = item.Value;//修改成员值，产生修改信息
                    if (!item.DontRenewBufMark)
                    {
                        m_paramDic[item.Code].Value = val;
                        _paramList.Add((ParameterUnit)m_paramDic[item.Code].ShallowClone());
                        //ChangeBuffer(m_paramDic[item.Code].Position, intValue: (ushort)Convert.ToInt16(m_paramDic[item.Code].Value));
                        ChangeBuffer(m_paramDic[item.Code].Position,  (ushort)Convert.ToInt16(m_paramDic[item.Code].m_value));
                    }
                    else
                    {
                        ParameterUnit pu = (ParameterUnit)m_paramDic[item.Code].ShallowClone();
                        pu.Value = val;
                        _paramList.Add(pu);

                    }




                    //if (!item.DontRenewBufMark)

                    //paramList.Remove(item);
                }
            }
            //获取写参数指令
            cmdList.AddRange(GetWriteDataCommands(null, _paramList));
            return cmdList;
        }

        //初始化系统参数
        public override List<CommUnit> InitCtx(object ctxData)
        {
            List<CommUnit> cmdList = new List<CommUnit>();
            List<ParameterUnit> _paramList = new List<ParameterUnit>();

            Dictionary<string, short> shortDic = ctxData as Dictionary<string, short>;
            if (shortDic != null)
            {
                foreach (var key in shortDic.Keys)
                {
                    if (m_paramDic.ContainsKey(key))
                    {
                        string val = CheckBoolValue(key).ToString();
                        if (m_paramDic[key].Value == val)
                        {
                            continue;
                        }

                        m_paramDic[key].Value = val;
                        _paramList.Add((ParameterUnit)m_paramDic[key].ShallowClone());

                        ChangeBuffer(m_paramDic[key].Position,  (ushort)Convert.ToInt16(m_paramDic[key].Value));

                    }
                }
                //获取写参数指令
                cmdList.AddRange(GetWriteDataCommands(null, _paramList));
            }


            return cmdList;
        }

 

        public override List<CommUnit> GetInitCmdList()
        {
            //获取初始化指令
            List<CommUnit> cmdList = new List<CommUnit>();

            //获取写参数指令
            if (m_paramList.Count > 0)
            {
                cmdList.AddRange(GetWriteDataCommands(null, m_paramList));
            }
            return cmdList;
        }

        //更新参数，用于plc参数监控
        public override void InputNewDatas(ushort[] data)//
        {
            m_msgLock = true;

            Queue<ParamUnit> puQ = new Queue<ParamUnit>();
            foreach (string item in m_paramDic.Keys)
            {
                try
                {
                    int pos = Convert.ToInt32(m_paramDic[item].Position);

                    string v = ((short)data[pos]).ToString();

                    if (m_paramDic[item].Value != v)
                    {
                        if (mytest)
                        {
                            if (posiList.ContainsKey(pos) == false)
                            {
                                posiList.Add(pos, new List<string>());
                            }
                            else//已包含对应键值
                            {
                                if (posiList[pos].Contains(v) == false)
                                {
                                    posiList[pos].Add(v);
                                }
                            }

                        }
                        m_paramDic[item].Value = v;
                        ParamUnit pu = m_paramDic[item].TransferToRemoteUnit();//生成过的参数队列
                        pu.Code = m_baseName + "." + pu.Code;
                        puQ.Enqueue(pu);
                    }
                    else
                    {
                        m_paramDic[item].Value = v;
                    }
                }
                catch (Exception ex)
                {

                    throw;
                }



            }
            //if (puQ.Count > 0 && OnRenewParamListEvent != null)
            //{
            //    OnRenewParamListEvent(puQ);
            //}

            puQ = null;
            m_msgLock = false;
        }

        //获取写参数指令
        public override List<CommUnit> GetWriteDataCommands(Dictionary<string, ushort> dataList = null, List<ParameterUnit> paramList = null)//
        {
            List<CommUnit> cmdList = new List<CommUnit>();
            if (paramList != null)
            {
                if (paramList.Count > 0)
                {
                    foreach (ParameterUnit item in paramList)
                    {
                        cmdList.Add(new CommUnit(CommAction.WriteMultReg, Convert.ToUInt16(item.Position), new ushort[1] { (UInt16)Convert.ToInt16(CheckBoolValue(item.Value)) }));
                    }
                }
            }
            return cmdList;
        }

        #endregion====================================================================


        #region============================= 消息响应 ================================
        #endregion====================================================================
    }
}
